from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import Qt
import sys
 
class QLine_comprehensive_case(QWidget):
    def __init__(self):
        super(QLine_comprehensive_case, self).__init__()
        self.initUI()
 
    def initUI(self):
        '''
        控件定义及属性设置
        '''
        edit1=QLineEdit()
        #int校验器
        edit1.setValidator(QIntValidator())
        #设置最大位数为4位，即不超过9999
        edit1.setMaxLength(4)
        #右对齐
        edit1.setAlignment(Qt.AlignRight)
        #这个字号直接影响输入框的尺寸，可以自己调整到其他尺寸感受一下
        edit1.setFont(QFont('Arial',20))
 
        edit2=QLineEdit()
        edit2.setValidator(QDoubleValidator(0.99,99.99,2))#2是精度，即小数点后几位
 
        edit3=QLineEdit()
        edit3.setInputMask('99_9999_999999;#')
 
        edit4=QLineEdit()
        edit4.textChanged.connect(self.textChanged) #绑定信号和槽
 
        edit5=QLineEdit()
        edit5.setEchoMode(QLineEdit.Password)
        edit5.editingFinished.connect(self.enterPress) #绑定信号和槽
 
        edit6=QLineEdit('Hello Pyqt5!')
        edit6.setReadOnly(True) #只读
 
        '''
        将控件添加到表单
        '''
        formlayout=QFormLayout()
        formlayout.addRow('整数校验',edit1)
        formlayout.addRow('浮点数校验',edit2)
        formlayout.addRow('input mask',edit3)
        formlayout.addRow('文本变化',edit4)
        formlayout.addRow('密码', edit5)
        formlayout.addRow('只读', edit6)
 
        '''
        设置主窗口属性
        '''
        self.setLayout(formlayout)
        self.setWindowTitle('QLineEdit综合案例')
 
    '''
    槽函数
    '''
    def textChanged(self,text):
        print('输入的内容'+text)
    def enterPress(self):
        print('已输入值')
 
 
if __name__=='__main__':
    app=QApplication(sys.argv)
    main=QLine_comprehensive_case()
    main.show()
    sys.exit(app.exec_())
